// 初始化第一个 MongoDB 实例
print('Initializing MongoDB Instance 1...');

// 等待数据库启动
sleep(5000);

// 初始化副本集
try {
  rs.initiate({
    _id: "rs0",
    members: [
      { _id: 0, host: "localhost:27017" }
    ]
  });
  print('Replica set initialized successfully for instance 1');
} catch (error) {
  print('Replica set already initialized or error:', error.message);
}

// 等待副本集选举完成
sleep(3000);

// 创建测试数据库和集合
db = db.getSiblingDB('testdb');

// 创建用户集合
db.createCollection('users');
db.users.createIndex({ "email": 1 }, { unique: true });

// 插入测试用户数据
db.users.insertMany([
  {
    _id: ObjectId(),
    name: "张三",
    email: "zhangsan@example.com",
    age: 25,
    city: "北京",
    created_at: new Date(),
    tags: ["技术", "开发"]
  },
  {
    _id: ObjectId(),
    name: "李四",
    email: "lisi@example.com",
    age: 30,
    city: "上海",
    created_at: new Date(),
    tags: ["管理", "产品"]
  },
  {
    _id: ObjectId(),
    name: "王五",
    email: "wangwu@example.com",
    age: 28,
    city: "深圳",
    created_at: new Date(),
    tags: ["设计", "UI"]
  }
]);

// 创建产品集合
db.createCollection('products');
db.products.createIndex({ "sku": 1 }, { unique: true });

// 插入测试产品数据
db.products.insertMany([
  {
    _id: ObjectId(),
    name: "笔记本电脑",
    sku: "LAPTOP001",
    price: 5999.00,
    category: "电子产品",
    stock: 50,
    created_at: new Date(),
    features: ["高性能", "轻薄", "长续航"]
  },
  {
    _id: ObjectId(),
    name: "智能手机",
    sku: "PHONE001",
    price: 3999.00,
    category: "电子产品",
    stock: 100,
    created_at: new Date(),
    features: ["5G", "大屏", "快充"]
  },
  {
    _id: ObjectId(),
    name: "无线耳机",
    sku: "HEADPHONE001",
    price: 899.00,
    category: "配件",
    stock: 200,
    created_at: new Date(),
    features: ["降噪", "蓝牙5.0", "长续航"]
  }
]);

// 创建订单集合
db.createCollection('orders');
db.orders.createIndex({ "order_number": 1 }, { unique: true });

// 插入测试订单数据
db.orders.insertMany([
  {
    _id: ObjectId(),
    order_number: "ORD2024001",
    user_id: db.users.findOne({email: "zhangsan@example.com"})._id,
    products: [
      {
        product_id: db.products.findOne({sku: "LAPTOP001"})._id,
        quantity: 1,
        price: 5999.00
      }
    ],
    total_amount: 5999.00,
    status: "已完成",
    created_at: new Date(),
    shipping_address: {
      street: "中关村大街1号",
      city: "北京",
      zip_code: "100080"
    }
  },
  {
    _id: ObjectId(),
    order_number: "ORD2024002",
    user_id: db.users.findOne({email: "lisi@example.com"})._id,
    products: [
      {
        product_id: db.products.findOne({sku: "PHONE001"})._id,
        quantity: 1,
        price: 3999.00
      },
      {
        product_id: db.products.findOne({sku: "HEADPHONE001"})._id,
        quantity: 2,
        price: 899.00
      }
    ],
    total_amount: 5797.00,
    status: "处理中",
    created_at: new Date(),
    shipping_address: {
      street: "南京路100号",
      city: "上海",
      zip_code: "200000"
    }
  }
]);

print('Instance 1 initialization completed!');
print('Created collections: users, products, orders');
print('Database: testdb');
print('Connection: localhost:20727'); 